<template>
    <div class="statics-file">
      <el-table 
        :data="formatIndexData" 
        :header-cell-style="tableHeaderColor"
        :span-method="cellMerge1"
        v-loading="listLoading"
        style="width: 100%;" 
        border
      >
        <el-table-column label="上传用户" prop="username" min-width="150" align="left"></el-table-column>
        <el-table-column label="文件名称" min-width="300" align="left">
          <template slot-scope="scope">
            <div class="fileinfo">

              <image-type v-if="scope.row.fileType" :fileType="scope.row.fileType" class="filename">
                {{ scope.row.file | filenameFilter }}
                <div class="toolBox">
                  <!-- <el-button type="text" @click="previewFile(scope.row)">查看</el-button> -->
                  <el-dropdown size="small" placement="bottom" @command="value => handleCommand(value, scope.row)">
              <span class="el-dropdown-link"> <i class="el-icon-more el-icon--right"></i>  </span>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item command="preview" icon="el-icon-view">查看</el-dropdown-item>
                <el-dropdown-item command="push" icon="el-icon-share">{{ scope.row.is_msg ? '取消推送': '推送' }}</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
                </div>
              </image-type>
            </div>
            <!-- <img :src="require('@/assets/images/'+ fileFilter(scope.row.fileType) +'.png')" alt="" class="file-img">{{ scope.row.file }} -->
          </template>
        </el-table-column>
        <el-table-column label="上传时间" min-width="110">
          <template slot-scope="scope">{{ parseTime(scope.row.create_time) }}</template>
        </el-table-column>
      </el-table>

      <el-dialog title="痕迹资料点评" :visible.sync="dialogFormVisible" append-to-body>
          <div v-if="abstract" class="desc" style="padding-bottom: 10px;">
            正在点评痕迹 》{{ abstract }} 
          </div>
          <el-form :model="comment">
            <el-form-item label="点评内容">
              <el-input type="textarea" :rows="3" v-model="comment.content" autocomplete="off" style="width: 86%;"></el-input>
            </el-form-item>
            <el-form-item label="点评意见">
              <el-radio-group size="small" v-model="comment.status">
                  <el-radio label="合格">合格</el-radio>
                  <el-radio label="基本合格">基本合格</el-radio>
                  <el-radio label="不合格">不合格</el-radio>
                </el-radio-group>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button size="small" @click="dialogFormVisible = false">取 消</el-button>
            <el-button size="small" type="primary" @click="postComment">确 定</el-button>
          </div>
        </el-dialog>
    </div>
</template>
<script>
  import { mapGetters } from 'vuex'
import { parseTime, getFileType, previewFile, formatTime } from "@/utils/index";
import handleBatchDownload from '@/utils/export2Zip'
import { getStatistFile } from "@/api/work.js";
import { updateTaskCenterFile } from "@/api/document";
import ImageType from '@/components/ImageType'
export default {
  name: "StaticsFile",
  components:{
      ImageType
    },
  props:{
    targetId:{
      type: String,
      require: true
    }
  },

  data() {
    return {
      spanArr: [],
      pos: 0,
      listLoading: false,
      dialogFormVisible: false,
      comment: {}, // 点评信息
      abstract: '',
      markId: null,
      fileUrlList: [], // 文件地址列表
      formatIndexData:[]
    };
  },
  created() {
    this.fetchMarkInfo()
  },
  methods: {
    parseTime,
    previewFile,
    downloadZip(name){
      // if(!this.fileUrlList.length){
      //   this.$message.warning('暂无上报文件可以导出')
      //   this.$emit('isDataNull', true)
      //   return
      // }
      handleBatchDownload(this.fileUrlList, name, this.handComplete)
    },
    handleCommand(command, row) {
      switch(command) {
        case 'preview': 
          this.previewFile(row)
          break
        case 'push': 
          this.handlePushFile(row)
          break
      }
    },
    /**
     * 将文件推送至公告通知
     */
    async handlePushFile(row) {
      const result = await updateTaskCenterFile(row.id, { is_msg: !row.is_msg });
      if(result) {
        this.$message.success(`${row.is_msg ? '取消': ''}推送文件到公告通知成功`)
        this.fetchMarkInfo()
      }
    },
    handComplete(falg){
      this.$emit('completFunc', falg)
    },
    fetchMarkInfo() {
      this.listLoading = true
      getStatistFile(this.targetId).then(res => {
        const data = res.data.result
        if(!data.length){
          this.$emit('isDataNull', true)
        }
        this.getSpanArr(this.flattenMark(data))
        this.formatIndexData = data
        this.listLoading = false
      });
    },
    flattenMark(list) {
      const result = []
        list.forEach(e => {
            result.push(decodeURI(e.url))
            e.file = e.url
            e.fileType = getFileType(e.url, '.')
            e.url = process.env.VUE_APP_IMAGE_URL  + e.url
        })
        this.fileUrlList = result
        return list;
      },
    // 点评窗口
    handComment(id, val){
      this.dialogFormVisible = true
      this.abstract = val
      this.markId = id
    },
    postComment(){
      const param = {
        content: this.comment.content,
        status: this.comment.status,
        mark: this.markId,
        owner: this.id
      }
      postMarkComent(param).then(res=>{
        if(res && res.data){
          this.$message.success('痕迹点评成功')
          this.dialogFormVisible = false
        }
      })

    },
    getSpanArr(data) {　
  for (var i = 0; i < data.length; i++) {
        if (i === 0) {
              this.spanArr.push(1);
              this.pos = 0
        } else {
          // 判断当前元素与上一个元素是否相同
    if (data[i].username === data[i - 1].username) {
                this.spanArr[this.pos] += 1;
                this.spanArr.push(0);
              } else {
                this.spanArr.push(1);
                this.pos = i;
              }
        }
    }
},
    cellMerge1({ row, column, rowIndex, columnIndex }) {
      if (columnIndex === 0 || columnIndex === 3) {
            const _row = this.spanArr[rowIndex];
            const _col = _row > 0 ? 1 : 0;
            return {
                  rowspan: _row,
                  colspan: _col
            }
      }
},
    
    cellMerge({ row, column, rowIndex, columnIndex }) {
      const span = column["property"] + "-span";
      if (row[span]) {
        return row[span];
      }
    },
    mergeTableRow(data, merge) {
      if (!merge || merge.length === 0) {
        return data;
      }
      merge.forEach(m => {
        const mList = {};
        data = data.map((v, index) => {
          const rowVal = v[m];
          if (mList[rowVal] && mList[rowVal].newIndex === index) {
            mList[rowVal]["num"]++;
            mList[rowVal]["newIndex"]++;
            data[mList[rowVal]["index"]][m + "-span"].rowspan++;
            v[m + "-span"] = {
              rowspan: 0,
              colspan: 0
            };
          } else {
            mList[rowVal] = { num: 1, index: index, newIndex: index + 1 };
            v[m + "-span"] = {
              rowspan: 1,
              colspan: 1
            };
          }
          return v;
        });
      });
      return data;
    },
    // 修改table header的背景色
    tableHeaderColor({ row, rowIndex }) {
    return 'background-color: #f5f5f5;color: #666;'
    },
  }
};
</script>
<style lang="scss">
.statics-file{
  .markname{
    .sendbtn{
      display: none;
    }
    &:hover{
      .sendbtn{
        display: inline-block;
      }
    }
  }
  .fileinfo{

    .toolBox{
      color: #6f6f6f;
      margin-left: 10px;
      display: none;
    }
    &:hover{
      .toolBox{
        display: inline-block;
      }
    }
  }

    .el-button--text{
      padding: 0;
    }
  }
      .el-dialog__body{
        padding: 10px 20px;
      }
</style>
